package org.ww.order.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ww.common.constant.RabbitMqConstant;
import org.ww.order.entity.OrderEntity;
import org.ww.order.service.OrderService;

import java.io.IOException;

/**
 * @program: gmall
 * @description:
 * @author: ww
 * @create: 2024-10-31 16:11
 **/
@Slf4j
@Service
@RabbitListener(queues = RabbitMqConstant.ORDER_RELEASE_ORDER_QUEUE)
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;
    @RabbitHandler
    public void handlerOrderCancelMessage(Message message, Channel channel, OrderEntity entity) throws IOException {
        log.info("收到订单过期消息：【{}】",entity);
        try{
            orderService.orderClose(entity);
            //收单的时候，如果用户在最后时间进行支付，同时mq中的订单过期处理订单状态，这样的话，订单数据就不准确了
            //这里可以手动调用支付宝收单功能（参考支付宝api）
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
}
